package com.qingzhuge.manager.security.handler;

import cn.hutool.http.HttpStatus;
import com.google.gson.Gson;
import com.qingzhuge.common.HttpCode;
import com.qingzhuge.controller.base.AbstractController;
import com.qingzhuge.dto.response.ResponseBodyDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/**
 * @author zeroxiao
 * @date 2019/9/11 21:37
 * 注销成功hanlder
 */
@Slf4j
public class LogoutSuccessHandler implements org.springframework.security.web.authentication.logout.LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.info("退出成功:[{}]",authentication);
        AbstractController.setUsername(null);
        AbstractController.setUid(null);
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        ResponseBodyDto<String> result = new ResponseBodyDto<>();
        result.setCode(HttpCode.OK.value());
        result.setMessage(HttpCode.OK.message());
        response.setStatus(HttpStatus.HTTP_OK);
        PrintWriter printWriter = response.getWriter();
        printWriter.append(new Gson().toJson(result));
    }
}
